package it.okkam.geosparql;

import it.okkam.strutturedati.Extent;
import it.okkam.utility.UtilityQuery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.tdb.TDBFactory;

public class GeoGetExtentsParticelleComuniInterressati {
	
	public static HashMap<String, Dataset> dataSetComuni = new HashMap<String, Dataset>();
	
	public static void loadDataSetComuni (String comuneCodice){
		UtilityQuery utq = new UtilityQuery();
		String comune = utq.getComune(comuneCodice); 
		if (!dataSetComuni.containsKey(comune)){
			Dataset dsComune = TDBFactory.assembleDataset("/home/okkam/Nicola/assemblers/"+comune.toLowerCase()+"-assembler.ttl");
			dataSetComuni.put(comune, dsComune);
		}
		}
	public HashMap<String, HashMap<String , List<Extent>>> getExtentsParticelleComuniInterressati (Map<String, Set<String>>  cambiamentoComuni ){
		UtilityQuery utq = new UtilityQuery();
		HashMap<String, HashMap<String , List<Extent>>> mappaOut = new HashMap<String, HashMap<String , List<Extent>>>();
		List<Extent> listaExtent ;
		Set keys = cambiamentoComuni.keySet(); // The set of keys in the map.
		Iterator keyIter = keys.iterator();
		HashMap<String, List<Extent>> extents = new HashMap<String, List<Extent>>();
		while (keyIter.hasNext()) {			 
			String idCambiamento = (String) keyIter.next();
			Set codiceComuneCatastale = (Set) cambiamentoComuni.get(idCambiamento); 
			for(Object cccObject: codiceComuneCatastale){
				String ccc = cccObject.toString();
				if (extents.containsKey(ccc)){
					continue;
				}
				else {
					String dataSet = utq.getDataSet(ccc);
					String comuneUri = utq.getUriComune(ccc);
					String comuneString = utq.getComune(ccc);
					loadDataSetComuni(ccc);
					String grafoComune = utq.getNomeGrafo(comuneString);
					Dataset dsComune = dataSetComuni.get(comuneString);
					Model modelExtent = dsComune.getNamedModel(grafoComune) ;
					
					String queryString = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> ";
					queryString=queryString+" PREFIX pc: <http://www.okkam.it/ont/particella_catastale.owl#>";
					queryString=queryString+" SELECT ?particella  ?extentMin ?extentMax ?extentMinLat ?extentMinLong ?extentMaxLat ?extentMaxLong ";
					queryString=queryString+" FROM <"+ grafoComune + "> ";
					queryString=queryString+" WHERE { ?particella a pc:Particella . " ;
					queryString=queryString+"?particella pc:haExtentMin ?extentMin . ";
					queryString=queryString+"?particella pc:haExtentMax ?extentMax . ";
					queryString=queryString+"?extentMin geo:lat ?extentMinLat . ";
					queryString=queryString+"?extentMin geo:long ?extentMinLong . ";
					queryString=queryString+"?extentMax geo:lat ?extentMaxLat . ";
					queryString=queryString+"?extentMax geo:long ?extentMaxLong  ";
					queryString=queryString+"}";
					com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
					QueryExecution qexec = QueryExecutionFactory.create(query, modelExtent);
					//Geo.setContext(qexec, index);
			        ResultSet results = qexec.execSelect();
			        //System.out.println(queryString+" query terminata" );
			       
			        //Extent ext;
			        
			        int i =1;
			        listaExtent = new ArrayList<Extent>(); 
			        for (; results.hasNext();) {
			        	QuerySolution record = results.next();
			        	String extentMin = record.get("extentMin").toString();
			        	i++;
			        	String extentMax = record.get("extentMax").toString();
			        	String extentMinLat = utq.getCoordinata(record.get("extentMinLat").toString());
			        	String extentMinLong = utq.getCoordinata(record.get("extentMinLong").toString());
			        	String extentMaxLat = utq.getCoordinata(record.get("extentMaxLat").toString());
			        	String extentMaxLong = utq.getCoordinata(record.get("extentMaxLong").toString());
			        	Extent extentQuery = new Extent(); 	
			        	extentQuery.setxMin(Double.parseDouble(extentMinLat));
			        	extentQuery.setyMin(Double.parseDouble(extentMinLong));
			        	extentQuery.setxMax(Double.parseDouble(extentMaxLat));
			        	extentQuery.setyMax(Double.parseDouble(extentMaxLong));      	
			        	listaExtent.add(extentQuery);
			        }
			        
				}
				if (listaExtent.size()>0)						
					extents.put(ccc, listaExtent);
				

				}
			mappaOut.put(idCambiamento, extents);
			}
			
			
		
			
		return mappaOut ;}
}

